﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Configuration;

namespace Font_Web_Service
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Font_Web_Service : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public FontSchema.StringMeasurementsDataTable calculatePixelLengths(FontSchema.StringMeasurementsDataTable fontsToCalculate, Int32 width, Int32 height)
        {
            if (null == fontsToCalculate || fontsToCalculate.Count == 0)
            {
                return fontsToCalculate;
            }

            try
            {

                foreach (FontSchema.StringMeasurementsRow fontRow in fontsToCalculate)
                {
                    FontStyle currStyle = FontStyle.Regular;
                    if (fontRow.Bold)
                    {
                        currStyle = currStyle | FontStyle.Bold;
                    }
                    if (fontRow.Italic)
                    {
                        currStyle = currStyle | FontStyle.Italic;
                    }

                    Font currFont = new Font(fontRow.Font, fontRow.Size, currStyle);
                    SizeF currStringSize = new SizeF();
                    //Size proposedSize = new Size(385, fontRow.Size);
                    currStringSize = TextRenderer.MeasureText(fontRow.Text, currFont);
                    //fontRow.Pixels = Convert.ToInt32(Convert.ToDouble(currStringSize.Width) * fontRow.Ratio);
                    Double pixelCount = currStringSize.Width * fontRow.Ratio;
                    Double roundedPixelCount = Math.Round(pixelCount);
                    if (pixelCount > roundedPixelCount)
                    {
                        roundedPixelCount++;
                    }
                    fontRow.Pixels = Convert.ToInt32(roundedPixelCount);

                }

                return fontsToCalculate;

            }
            catch (Exception ex)
            {

            }

            return null;
        }

    }
}
